home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / Glypha III Src / Code / Prefs.c < prev    next >
Encoding:
Text File  |  1995-01-29  |  10.2 KB  |  489 lines  |  [TEXT/MMCC]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                    Prefs.c
  5. //----------------------------------------------------------------------------
  6. //============================================================================
  7.  
  8.  
  9. #include "Externs.h"
  10. #include <Folders.h>
  11. #include <GestaltEqu.h>
  12. #include <Script.h>
  13.  
  14.  
  15. #define    kPrefCreatorType    'zade'
  16. #define    kPrefFileType        'zadP'
  17. #define    kPrefFileName        "\pGlypha Prefs"
  18. #define    kDefaultPrefFName    "\pPreferences"
  19. #define kPrefsStringsID        160
  20. #define    kPrefsFNameIndex    1
  21.  
  22.  
  23. Boolean CanUseFindFolder (void);
  24. Boolean GetPrefsFPath (long *, short *);
  25. Boolean CreatePrefsFolder (short *);
  26. Boolean GetPrefsFPath6 (short *);
  27. Boolean WritePrefs (long *, short *, prefsInfo *);
  28. Boolean WritePrefs6 (short *, prefsInfo *);
  29. OSErr ReadPrefs (long *, short *, prefsInfo *);
  30. OSErr ReadPrefs6 (short *, prefsInfo *);
  31. Boolean DeletePrefs (long *, short *);
  32. Boolean DeletePrefs6 (short *);
  33.  
  34.  
  35. //==============================================================  Functions
  36. //--------------------------------------------------------------  CanUseFindFolder
  37.  
  38. Boolean CanUseFindFolder (void)
  39. {
  40.     OSErr        theErr;
  41.     long        theFeature;
  42.     
  43.     if (!DoWeHaveGestalt())
  44.         return(FALSE);
  45.     
  46.     theErr = Gestalt(gestaltFindFolderAttr, &theFeature);
  47.     if (theErr != noErr)
  48.         return(FALSE);
  49.     
  50.     if (!BitTst(&theFeature, 31 - gestaltFindFolderPresent))
  51.         return(FALSE);
  52.     else
  53.         return(TRUE);
  54. }
  55.  
  56. //--------------------------------------------------------------  GetPrefsFPath
  57.  
  58. Boolean GetPrefsFPath (long *prefDirID, short *systemVolRef)
  59. {
  60.     OSErr        theErr;
  61.     
  62.     theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, 
  63.         systemVolRef, prefDirID);
  64.     if (theErr != noErr)
  65.         return(FALSE);
  66.     
  67.     return(TRUE);
  68. }
  69.  
  70. //--------------------------------------------------------------  CreatePrefsFolder
  71.  
  72. Boolean CreatePrefsFolder (short *systemVolRef)
  73. {
  74.     HFileParam    fileParamBlock;
  75.     Str255        folderName;
  76.     OSErr        theErr;
  77.     
  78.     GetIndString(folderName, kPrefsStringsID, kPrefsFNameIndex);
  79.     
  80.     fileParamBlock.ioVRefNum = *systemVolRef;
  81.     fileParamBlock.ioDirID = 0;
  82.     fileParamBlock.ioNamePtr = folderName;
  83.     fileParamBlock.ioCompletion = 0L;
  84.     
  85.     theErr = PBDirCreate((HParmBlkPtr)&fileParamBlock, FALSE);
  86.     if (theErr != noErr)
  87.     {
  88.         RedAlert("\pPrefs Creation Error");
  89.         return(FALSE);
  90.     }
  91.     return(TRUE);
  92. }
  93.  
  94. //--------------------------------------------------------------  GetPrefsFPath6
  95.  
  96. Boolean GetPrefsFPath6 (short *systemVolRef)
  97. {
  98.     Str255        folderName;
  99.     SysEnvRec    thisWorld;
  100.     CInfoPBRec    catalogInfoPB;
  101.     DirInfo        *directoryInfo = (DirInfo *) &catalogInfoPB;
  102.     HFileInfo    *fileInfo = (HFileInfo *) &catalogInfoPB;
  103.     WDPBRec        workingDirPB;
  104.     long        prefDirID;
  105.     OSErr        theErr;
  106.     
  107.     GetIndString(folderName, kPrefsStringsID, kPrefsFNameIndex);
  108.     
  109.     theErr = SysEnvirons(2, &thisWorld);
  110.     if (theErr != noErr)
  111.         return(FALSE);
  112.         
  113.     *systemVolRef = thisWorld.sysVRefNum;
  114.     fileInfo->ioVRefNum = *systemVolRef;
  115.     fileInfo->ioDirID  = 0;
  116.     fileInfo->ioFDirIndex = 0;
  117.     fileInfo->ioNamePtr = folderName;
  118.     fileInfo->ioCompletion = 0L;
  119.     theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
  120.     if (theErr != noErr)
  121.     {
  122.         if (theErr != fnfErr)
  123.         {
  124.             RedAlert("\pPrefs Filepath Error");
  125.         }
  126.         if (!CreatePrefsFolder(systemVolRef))
  127.             return(FALSE);
  128.     
  129.         directoryInfo->ioVRefNum = *systemVolRef;
  130.         directoryInfo->ioFDirIndex = 0;
  131.         directoryInfo->ioNamePtr = folderName;
  132.         theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
  133.         if (theErr != noErr)
  134.         {
  135.             RedAlert("\pPrefs GetCatInfo() Error");
  136.             return(FALSE);
  137.         }
  138.     }
  139.     prefDirID = directoryInfo->ioDrDirID;
  140.     
  141.     workingDirPB.ioNamePtr = nil;
  142.     workingDirPB.ioVRefNum = *systemVolRef;
  143.     workingDirPB.ioWDIndex = 0;
  144.     workingDirPB.ioWDProcID = 0;
  145.     workingDirPB.ioWDVRefNum = 0;
  146.     workingDirPB.ioCompletion = 0L;
  147.     theErr = PBGetWDInfo(&workingDirPB, FALSE);
  148.     if (theErr != noErr)
  149.     {
  150.         RedAlert("\pPrefs PBGetWDInfo() Error");
  151.     }
  152.         
  153.     *systemVolRef = workingDirPB.ioWDVRefNum;
  154.     
  155.     workingDirPB.ioNamePtr = nil;
  156.     workingDirPB.ioWDDirID = prefDirID;
  157.     workingDirPB.ioVRefNum = *systemVolRef;
  158.     workingDirPB.ioWDProcID = 0;
  159.     workingDirPB.ioCompletion = 0L;
  160.     theErr = PBOpenWD(&workingDirPB, FALSE);
  161.     if (theErr != noErr)
  162.     {
  163.         RedAlert("\pPrefs PBOpenWD() Error");
  164.     }
  165.     
  166.     *systemVolRef = workingDirPB.ioVRefNum;
  167.     
  168.     return(TRUE);
  169. }
  170.  
  171. //--------------------------------------------------------------  WritePrefs
  172.  
  173. Boolean WritePrefs (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)
  174. {
  175.     OSErr        theErr;
  176.     short        fileRefNum;
  177.     long        byteCount;
  178.     FSSpec        theSpecs;
  179.     Str255        fileName = kPrefFileName;
  180.     
  181.     theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
  182.     if (theErr != noErr)
  183.     {
  184.         if (theErr != fnfErr)
  185.         {
  186.             RedAlert("\pPrefs FSMakeFSSpec() Error");
  187.         }
  188.         theErr = FSpCreate(&theSpecs, kPrefCreatorType, kPrefFileType, smSystemScript);
  189.         if (theErr != noErr)
  190.         {
  191.             RedAlert("\pPrefs FSpCreate() Error");
  192.         }
  193.     }
  194.     theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
  195.     if (theErr != noErr)
  196.     {
  197.         RedAlert("\pPrefs FSpOpenDF() Error");
  198.     }
  199.     
  200.     byteCount = sizeof(*thePrefs);
  201.     
  202.     theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
  203.     if (theErr != noErr)
  204.     {
  205.         RedAlert("\pPrefs FSWrite() Error");
  206.     }
  207.     
  208.     theErr = FSClose(fileRefNum);
  209.     if (theErr != noErr)
  210.     {
  211.         RedAlert("\pPrefs FSClose() Error");
  212.     }
  213.     
  214.     return(TRUE);
  215. }
  216.  
  217. //--------------------------------------------------------------  WritePrefs6
  218.  
  219. Boolean WritePrefs6 (short *systemVolRef, prefsInfo *thePrefs)
  220. {
  221.     OSErr        theErr;
  222.     short        fileRefNum;
  223.     long        byteCount;
  224.     Str255        fileName = kPrefFileName;
  225.     
  226.     theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
  227.     if (theErr != noErr)
  228.     {
  229.         if (theErr != fnfErr)
  230.         {
  231.             RedAlert("\pPrefs FSOpen() Error");
  232.         }
  233.         theErr = Create(fileName, *systemVolRef, kPrefCreatorType, kPrefFileType);
  234.         if (theErr != noErr)
  235.         {
  236.             RedAlert("\pPrefs Create() Error");
  237.         }
  238.         theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
  239.         if (theErr != noErr)
  240.         {
  241.             RedAlert("\pPrefs FSOpen() Error");
  242.         }
  243.     }
  244.     
  245.     byteCount = sizeof(*thePrefs);
  246.     
  247.     theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
  248.     if (theErr != noErr)
  249.     {
  250.         RedAlert("\pPrefs FSWrite() Error");
  251.     }
  252.     
  253.     theErr = FSClose(fileRefNum);
  254.     if (theErr != noErr)
  255.     {
  256.         RedAlert("\pPrefs FSClose() Error");
  257.     }
  258.     
  259.     return(TRUE);
  260. }
  261.  
  262. //--------------------------------------------------------------  SavePrefs
  263.  
  264. Boolean SavePrefs (prefsInfo *thePrefs, short versionNow)
  265. {
  266.     long        prefDirID;
  267.     short        systemVolRef;
  268.     Boolean        canUseFSSpecs;
  269.     
  270.     thePrefs->prefVersion = versionNow;
  271.     
  272.     canUseFSSpecs = CanUseFindFolder();
  273.     if (canUseFSSpecs)
  274.     {
  275.         if (!GetPrefsFPath(&prefDirID, &systemVolRef))
  276.             return(FALSE);
  277.     }
  278.     else
  279.     {
  280.         if (!GetPrefsFPath6(&systemVolRef))
  281.             return(FALSE);
  282.     }
  283.     
  284.     if (canUseFSSpecs)
  285.     {
  286.         if (!WritePrefs(&prefDirID, &systemVolRef, thePrefs))
  287.             return(FALSE);
  288.     }
  289.     else
  290.     {
  291.         if (!WritePrefs6(&systemVolRef, thePrefs))
  292.             return(FALSE);
  293.     }
  294.     
  295.     return(TRUE);
  296. }
  297.  
  298. //--------------------------------------------------------------  ReadPrefs
  299.  
  300. OSErr ReadPrefs (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)
  301. {
  302.     OSErr        theErr;
  303.     short        fileRefNum;
  304.     long        byteCount;
  305.     FSSpec        theSpecs;
  306.     Str255        fileName = kPrefFileName;
  307.     
  308.     theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
  309.     if (theErr != noErr)
  310.     {
  311.         if (theErr == fnfErr)
  312.             return(theErr);
  313.         else
  314.         {
  315.             RedAlert("\pPrefs FSMakeFSSpec() Error");
  316.         }
  317.     }
  318.     
  319.     theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
  320.     if (theErr != noErr)
  321.     {
  322.         RedAlert("\pPrefs FSpOpenDF() Error");
  323.     }
  324.     
  325.     byteCount = sizeof(*thePrefs);
  326.     
  327.     theErr = FSRead(fileRefNum, &byteCount, thePrefs);
  328.     if (theErr != noErr)
  329.     {
  330.         if (theErr == eofErr)
  331.             theErr = FSClose(fileRefNum);
  332.         else
  333.         {
  334.             RedAlert("\pPrefs FSRead() Error");
  335.         }
  336.         return(theErr);
  337.     }
  338.     
  339.     theErr = FSClose(fileRefNum);
  340.     if (theErr != noErr)
  341.     {
  342.         RedAlert("\pPrefs FSClose() Error");
  343.     }
  344.     
  345.     return(theErr);
  346. }
  347.  
  348. //--------------------------------------------------------------  ReadPrefs6
  349.  
  350. OSErr ReadPrefs6 (short *systemVolRef, prefsInfo *thePrefs)
  351. {
  352.     OSErr        theErr;
  353.     short        fileRefNum;
  354.     long        byteCount;
  355.     Str255        fileName = kPrefFileName;
  356.     
  357.     theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
  358.     if (theErr != noErr)
  359.     {
  360.         if (theErr == fnfErr)
  361.             return(theErr);
  362.         else
  363.         {
  364.             RedAlert("\pPrefs FSOpen() Error");
  365.         }
  366.     }
  367.     
  368.     byteCount = sizeof(*thePrefs);
  369.     
  370.     theErr = FSRead(fileRefNum, &byteCount, thePrefs);
  371.     if (theErr != noErr)
  372.     {
  373.         if (theErr == eofErr)
  374.             theErr = FSClose(fileRefNum);
  375.         else
  376.         {
  377.             RedAlert("\pPrefs FSRead() Error");
  378.         }
  379.         return(theErr);
  380.     }
  381.     
  382.     theErr = FSClose(fileRefNum);
  383.     if (theErr != noErr)
  384.     {
  385.         RedAlert("\pPrefs FSClose() Error");
  386.     }
  387.     
  388.     return(theErr);
  389. }
  390.  
  391. //--------------------------------------------------------------  DeletePrefs
  392.  
  393. Boolean DeletePrefs (long *dirID, short *volRef)
  394. {
  395.     FSSpec        theSpecs;
  396.     Str255        fileName = kPrefFileName;
  397.     OSErr        theErr;
  398.     
  399.     theErr = FSMakeFSSpec(*volRef, *dirID, fileName, &theSpecs);
  400.     if (theErr != noErr)
  401.         return(FALSE);
  402.     else
  403.         theErr = FSpDelete(&theSpecs);
  404.     
  405.     if (theErr != noErr)
  406.         return(FALSE);
  407.     
  408.     return(TRUE);
  409. }
  410.  
  411. //--------------------------------------------------------------  DeletePrefs6
  412.  
  413. Boolean DeletePrefs6 (short *volRef)
  414. {
  415.     Str255        fileName = kPrefFileName;
  416.     OSErr        theErr;
  417.     
  418.     theErr = FSDelete(fileName, *volRef);
  419.     
  420.     if (theErr != noErr)
  421.         return(FALSE);
  422.     
  423.     return(TRUE);
  424. }
  425.  
  426. //--------------------------------------------------------------  LoadPrefs
  427.  
  428. Boolean LoadPrefs (prefsInfo *thePrefs, short versionNeed)
  429. {
  430.     long        prefDirID;
  431.     OSErr        theErr;
  432.     short        systemVolRef;
  433.     Boolean        canUseFSSpecs, noProblems;
  434.     
  435.     canUseFSSpecs = CanUseFindFolder();
  436.     if (canUseFSSpecs)
  437.     {
  438.         noProblems = GetPrefsFPath(&prefDirID, &systemVolRef);
  439.         if (!noProblems)
  440.             return(FALSE);
  441.     }
  442.     else
  443.     {
  444.         noProblems = GetPrefsFPath6(&systemVolRef);
  445.         if (!noProblems)
  446.             return(FALSE);
  447.     }
  448.     
  449.     if (canUseFSSpecs)
  450.     {
  451.         theErr = ReadPrefs(&prefDirID, &systemVolRef, thePrefs);
  452.         if (theErr == eofErr)
  453.         {
  454.             noProblems = DeletePrefs(&prefDirID, &systemVolRef);
  455.             return(FALSE);
  456.         }
  457.         else if (theErr != noErr)
  458.             return(FALSE);
  459.     }
  460.     else
  461.     {
  462.         theErr = ReadPrefs6(&systemVolRef, thePrefs);
  463.         if (theErr == eofErr)
  464.         {
  465.             noProblems = DeletePrefs6(&systemVolRef);
  466.             return(FALSE);
  467.         }
  468.         else if (theErr != noErr)
  469.             return(FALSE);
  470.     }
  471.     
  472.     if (thePrefs->prefVersion != versionNeed)
  473.     {
  474.         if (canUseFSSpecs)
  475.         {
  476.             noProblems = DeletePrefs(&prefDirID, &systemVolRef);
  477.             return(FALSE);
  478.         }
  479.         else
  480.         {
  481.             noProblems = DeletePrefs6(&systemVolRef);
  482.             return(FALSE);
  483.         }
  484.     }
  485.     
  486.     return(TRUE);
  487. }
  488.  
  489.